package com.example.service.impl;

import com.example.entity.*;
import com.example.exception.BusinessException;
import com.example.exception.BusinessExceptionCode;
import com.example.mapper.ContentMapper;
import com.example.mapper.DocCustMapper;
import com.example.mapper.DocMapper;
import com.example.req.DocSaveReq;
import com.example.service.DocService;
import com.example.utils.CopyUtil;
import com.example.utils.RedisUtil;
import com.example.utils.RequestContext;
import com.example.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class DocServiceImpl implements DocService {

    @Autowired
    DocMapper docMapper;

    @Autowired
    SnowFlake snowFlake;

    @Autowired
    ContentMapper contentMapper;

    @Autowired
    DocCustMapper docCustMapper;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public List<Doc> findDoc(Long ebookId) {
        DocExample docExample = new DocExample();
        docExample.setOrderByClause("sort asc");
        docExample.createCriteria().andEbookIdEqualTo(ebookId);
        return docMapper.selectByExample(docExample);
    }

    @Override
    public void saveDoc(DocSaveReq req) {
        Content content = new Content();
        Doc doc = CopyUtil.copy(req, Doc.class);
        doc.setViewCount(0);
        doc.setVoteCount(0);

        if(ObjectUtils.isEmpty(doc.getId())){
            doc.setId(snowFlake.nextId());
            docMapper.insert(doc);
            content.setId(doc.getId());
            content.setContent(req.getContent());
            contentMapper.insert(content);
        }else{
            docMapper.updateByPrimaryKey(doc);
            content.setId(doc.getId());
            content.setContent(req.getContent());
            int update = contentMapper.updateByPrimaryKeyWithBLOBs(content);
            if(update == 0){
                contentMapper.insert(content);
            }
        }
    }

    @Override
    public void deleteDoc(Long id) {
        docMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteDocByList(List<Long> docList) {
        DocExample docExample = new DocExample();
        DocExample.Criteria docCriteria = docExample.createCriteria();
        docCriteria.andIdIn(docList);
        docMapper.deleteByExample(docExample);

        ContentExample contentExample = new ContentExample();
        ContentExample.Criteria exampleCriteria = contentExample.createCriteria();
        exampleCriteria.andIdIn(docList);
        contentMapper.deleteByExample(contentExample);
    }

    @Override
    public String findDocContent(Long id) {
        Content content = contentMapper.selectByPrimaryKey(id);
        if((content)==null)
            return "";
        return content.getContent();
    }

    @Override
    public void updateDocView(Long id){
        docCustMapper.updateDocView(id);
    }

    @Override
    public void updateDocVote(final Long id) {
//        docCustMapper.updateDocVote(id);
        String addr = RequestContext.getRemoteAddr();
        boolean validateResult = redisUtil.validateRepeat(addr + "_" + id, 3600 * 24);
        if(validateResult){
            docCustMapper.updateDocVote(id);
        }else{
            throw new BusinessException(BusinessExceptionCode.DOC_VOTE_ERROR);
        }
    }

    @Override
    public void updateEbookDocInfo(){
        docCustMapper.updateEbookDocInfo();
    }

}
